<?php

/**
 * @Author:      lyt
 * @DateTime:    2016-05-20 15:28:33
 * @Description: 零售订单的 控制器
 */
class ScsLSOrderController extends BaseController
{
    /**
     * @Author:      lyt
     * @DateTime:    2016-05-20 17:12:54
     * @Description: 创建零售订单
     */
    public function create(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'name' => 'max:50',
                'phone' => 'max:20',
                'car_no'=>'max:20|alpha_dash',
                'car_model_id'=>'max:20|alpha_dash',
                "mileage"=>'numeric',
                // "vin"=>'required',
                "note"=>'max:100',
                // "sex"=>'required',
                "insurance"=>'date',
                "user_id"=>'required'
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }

            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            /*
            if(Orders::createOrderBefore(Input::get('car_no'),$sc_user->shop_id,2)){
                $response = Lang::get("scs_error.order_create_error_40066");
                break;
            }
            */
            if(Input::get('user_id')!=0){
                $user=Users::find(Input::get('user_id'));
                if($user->status!=1){
                    $response = Lang::get("scs_error.order_cusuom_error_40004");
                    $response["result"]=$validator->messages()->all();
                    break;
                }
            }else{
                if(Input::get("phone")){
                    $user=Users::where("phone",Input::get("phone"))->first();
                }else{
                    $user=Users::find(Config::get('config.linshou_id'));
                }
            }

            if($user){//如果存在用户判断用户
                if($user->status!=1&&$user->id!=Config::get('config.linshou_id')){
                    $response = Lang::get("scs_error.order_cusuom_error_40004");
                    $response["result"]=$validator->messages()->all();
                    break;
                }
            }else{
                $user=new Users;
                $user->phone=Input::get("phone");
                $user->area=Input::get("area","010");
                $user->status=1;
                $user->register_source_id=14;//门店
                $user->member = Config::get('user.def_member');
                $user->sex=Input::get('sex');
                $user->save();
            }
            $csi=DataCarStandardInfo::find(Input::get("car_model_id"));
            if(Input::get("car_no")){//如果存在
                $uci=UserCarInfo::where("user_id",$user->id)->where("car_no",Input::get("car_no"))->where("status",1)->first();
                if($uci){//如果用户存在该车辆
                    ModifyCarsRecords::saveRecords($sc_user,$uci->id);
                }else{//如果不存在该车辆
                    $uci=new UserCarInfo;
                    $uci->car_no = strtoupper(Input::get('car_no'));
                    $uci->user_id = $user->id;
                    $uci->status = 1;
                    $uci->is_default = 0;
                    $uci->car_level = 0;
                    $uci->is_lock = 0;
                    $uci->mileage =0;//里程 为0 订单结束的是偶的时候更改用户的车辆里程
                }
                $uci->car_series_id = isset($csi)?$csi->series_name_id:0;
                $uci->car_brand_id = isset($csi)?$csi->brand_name_id:0;
                $uci->car_model_id = isset($csi)?$csi->id:0;
                $uci->vin_code = strtoupper(Input::get("vin",""));
                // $uci->mileage =0;//里程 为0 订单结束的是偶的时候更改用户的车辆里程
                $uci->note=Input::get("note");
                $uci->insurance=Input::get("insurance");
                $uci->start_off_time=Input::get("start_off_time");
                $uci->save();
            }

            if(Input::get('sex')==1){
                $tittle='先生';
            }elseif(Input::get('sex')==2){
                $tittle='女士';
            }else{
                $tittle='保密';
            }
            $orders = new Orders();
            $order_id = Common::createOrderId();
            $orders->order_id = $order_id;
            $orders->user_id =$user->id;
            $orders->city = $sc_user->shop->city_id;
            $orders->place = 2;
            $orders->book_time =date("Y-m-d",time()) ;
            $orders->book_hour ="";
            $orders->shop_id = $sc_user->shop_id;
            $orders->address = "";
            $orders->name = Input::get("name");
            $orders->tittle = $tittle;
            $orders->phone = Input::get("phone");
            $orders->pay_type = 0;
            $orders->user_car_id = isset($uci)?$uci->id:0;
            $orders->amount = 0;
            $orders->work_service =0;
            $orders->status =1411;
            $orders->note = Input::get("note")?Input::get("note"):"";
            $orders->car_brand_id = isset($csi)?$csi->brand_name_id:0;
            $orders->car_brand_name = isset($csi)?$csi->brand_name:"";
            $orders->car_series_id = isset($csi)?$csi->series_name_id:0;
            $orders->car_series_name = isset($csi)?$csi->series_name:'';
            $orders->car_model_id = isset($csi)?$csi->id:0;
            $orders->car_model_name = isset($csi)?$csi->model_name:'';
            $orders->car_no = isset($uci)?$uci->car_no:'';
            $orders->outer_order_id = 0;
            $orders->engineer_id = 0;
            $orders->car_id = 0;
            $mileage=Input::get('mileage');
            if(isset($mileage)){
                $orders->mileage = Input::get('mileage')?Input::get('mileage'):0;
            }
            $orders->oil_meter = 0;
            $orders->order_source = 14;//门店
            $orders->product_comment = "";
            $orders->book_type = 2;
            $orders->order_type = 2;
            $orders->reception_id = $sc_user->id;
            $orders->reception_name = $sc_user->real_name;
            $orders->save();

            // //进入停车场
            // $mp = new MaintenancePark();
            // $mp->shop_id=$sc_user->shop_id;
            // $mp->order_id=$order_id;
            // $mp->status=3;
            // $mp->user_id=$sc_user->id;
            // $mp->save();

            $order_flows = new OrderFlows;
            $order_flows->order_id = $order_id;
            $order_flows->status =1411;
            $order_flows->user_id = $sc_user->id;
            $order_flows->type = 'sc';
            $order_flows->save();

            // $order_flows = new OrderFlows;
            // $order_flows->order_id = $order_id;
            // $order_flows->status =1412;
            // $order_flows->user_id = $sc_user->id;
            // $order_flows->type = 'sc';
            // $order_flows->save();

            //记录行驶证信息
            try{
                if(Input::get('vin_code')!=""&&Input::get('car_no')!=""&&Input::get('send_time')!=""){
                    $dln=DrivingLicenseNew::where("car_no",Input::get('car_no'))->where("vin",Input::get('vin_code'))->where("send_time",Input::get('send_time'))->first();
                    if(is_null($dln)){//如果没有新建改记录
                        $dln=new DrivingLicenseNew();
                        $dln->shop_id = $sc_user->shop_id;
                        $dln->created_by=$sc_user->id;
                    }else{
                        $dln->updated_by=$sc_user->id;
                    }
                    $dln->img = Input::get('img');
                    $dln->car_no = Input::get('car_no');
                    $dln->car_type = Input::get('car_type');
                    $dln->own = Input::get('own');
                    $dln->address = Input::get('address');
                    $dln->model = Input::get('model');
                    $dln->vin = strtoupper(Input::get("vin_code"));
                    $dln->engine_number = Input::get('engine_number');
                    $dln->register_time = Input::get('register_time');
                    $dln->send_time = Input::get('send_time');
                    $dln->use_nature = Input::get('use_nature');
                    $dln->save();
                    $dlo=new DrivingLicenseOrder();
                    $dlo->order_id=$order_id;
                    $dlo->license_id=$dln->id;
                    $dlo->created_by=$sc_user->id;
                    $dlo->save();
                    $dls=DrivingLicenseShop::where("license_id",$dln->id)->where("shop_id",$sc_user->shop_id)->first();
                    if(is_null($dls)){
                        $dls=new DrivingLicenseShop();
                        $dls->license_id=$dln->id;
                        $dls->shop_id=$sc_user->shop_id;
                        $dls->created_by=$sc_user->id;
                        $dls->save();
                    }
                    $dlu=DrivingLicenseUser::where("license_id",$dln->id)->where("user_id",$user->id)->first();
                    if(is_null($dlu)){
                        $dlu=new DrivingLicenseUser();
                        $dlu->license_id=$dln->id;
                        $dlu->user_id=$user->id;
                        $dlu->created_by=$sc_user->id;
                        $dlu->save();
                    }
                    if(isset($uci)){
                        $temp_uci=UserCarInfo::find("id",$uci->id);
                        $temp_uci->vin_code=strtoupper(Input::get("vin_code"));
                        if(Input::get('register_time')){
                             $temp_uci->start_off_time=Input::get('register_time');
                        }                   
                        $temp_uci->save();
                        if($temp_uci->is_lock==1){//如果是认证车辆                       
                            UserAuthenticationCar::where('car_no',$uci->car_no)->where('status',1)->update(array("vin_code"=>strtoupper(Input::get("vin_code"))));
                        }
                    }
                }
            }catch(Exception $e){
                ;
            }

            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>$order_id));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "orderLs_create", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-05-21 11:26:26
     * @Description: 更改零售单联系人信息
     */
    public function update_order_user_info(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                // $messages = $validator->messages();
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=Orders::find(Input::get('order_id'));
            if(is_null($orders)||$orders->order_type!=2){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;

            }
            if(!in_array($orders->status,Orders::$chanage)){
                $response = Lang::get("scs_error.order_status_error_40005");
                break;

            }
            
            if(Input::get('sex')==1){
                $tittle='先生';
            }elseif(Input::get('sex')==2){
                $tittle='女士';
            }else{
                $tittle='保密';
            }
            //记录订单基本信息修改记录
            $obmr = new OrderBaseModifyRecord();
            $obmr->order_id=Input::get("order_id");
            $obmr->name=$orders->name;
            $obmr->tittle=$orders->tittle;
            $obmr->phone=$orders->phone;
            $obmr->mileage=$orders->mileage;
            $obmr->oil_meter=$orders->oil_meter;
            $obmr->address=$orders->address;
            $obmr->product_comment=$orders->product_comment;
            $obmr->car_brand_id=$orders->car_brand_id;
            $obmr->car_brand_name=$orders->car_brand_name;
            $obmr->car_series_id=$orders->car_series_id;
            $obmr->car_series_name=$orders->car_series_name;
            $obmr->car_model_id=$orders->car_model_id;
            $obmr->car_model_name=$orders->car_model_name;
            $obmr->user_id=$sc_user->id;
            $obmr->user_type='sc';
            $obmr->save();

            $orders->phone=Input::get('phone');
            $orders->name=Input::get('name');
            $orders->tittle=$tittle;
            $orders->save();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>Input::get('order_id')));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "lsorder_show", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      zwh
     * @DateTime:    2016-1-15 10:22:38
     * @Description: 修改订单车型信息
     */
    public function update_order_car_model(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'car_no'=>'max:20',
                'car_model_id'=>'max:20|alpha_dash',
                "mileage"=>'numeric',
                // "vin"=>'required',
                "note"=>'max:100',
                'order_id' => 'required'
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $csi=DataCarStandardInfo::find(Input::get("car_model_id"));
            $orders=Orders::find(Input::get('order_id'));
            if(is_null($orders)||$orders->order_type!=2){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;

            }
            if(!in_array($orders->status,array(1411))){
                $response = Lang::get("scs_error.order_status_error_40022");
                break;

            }
            if(Input::get("car_no")){//如果存在
                $uci=UserCarInfo::where("user_id",$orders->user_id)->where("car_no",Input::get("car_no"))->where("status",1)->first();
                if($uci){//如果用户存在该车辆
                    ModifyCarsRecords::saveRecords($sc_user,$uci->id);
                }else{//如果不存在该车辆
                    $uci=new UserCarInfo;
                    $uci->car_no = strtoupper(Input::get('car_no'));
                    $uci->user_id = $orders->user_id;
                    $uci->status = 1;
                    $uci->is_default = 0;
                    $uci->car_level = 0;
                    $uci->is_lock = 0;
                    $uci->mileage =0;//里程 为0 订单结束的是偶的时候更改用户的车辆里程
                }
                $uci->car_series_id = isset($csi)?$csi->series_name_id:0;
                $uci->car_brand_id = isset($csi)?$csi->brand_name_id:0;
                $uci->car_model_id = isset($csi)?$csi->id:0;
                $uci->vin_code = strtoupper(Input::get("vin",""));
                // $uci->mileage =0;//里程 为0 订单结束的是偶的时候更改用户的车辆里程
                $uci->note=Input::get("note");
                $uci->insurance=Input::get("insurance");
                $uci->start_off_time=Input::get('start_off_time');
                $uci->save();
            }



            //记录订单基本信息修改记录
            $obmr = new OrderBaseModifyRecord();
            $obmr->order_id=Input::get("order_id");
            $obmr->name=$orders->name;
            $obmr->phone=$orders->phone;
            $obmr->mileage=$orders->mileage;
            $obmr->oil_meter=$orders->oil_meter;
            $obmr->address=$orders->address;
            $obmr->product_comment=$orders->product_comment;
            $obmr->car_brand_id=$orders->car_brand_id;
            $obmr->car_brand_name=$orders->car_brand_name;
            $obmr->car_series_id=$orders->car_series_id;
            $obmr->car_series_name=$orders->car_series_name;
            $obmr->car_model_id=$orders->car_model_id;
            $obmr->car_model_name=$orders->car_model_name;
            $obmr->user_id=$sc_user->id;
            $obmr->user_type='sc';
            $obmr->save();

            $orders->note = Input::get("note","");
            $orders->car_brand_id = isset($csi)?$csi->brand_name_id:0;
            $orders->car_brand_name = isset($csi)?$csi->brand_name:"";
            $orders->car_series_id = isset($csi)?$csi->series_name_id:0;
            $orders->car_series_name = isset($csi)?$csi->series_name:'';
            $orders->car_model_id = isset($csi)?$csi->id:0;
            $orders->car_model_name = isset($csi)?$csi->model_name:'';
            $orders->car_no = isset($uci)?$uci->car_no:'';
            $orders->user_car_id = isset($uci)?$uci->id:0;
            $orders->mileage = Input::get('mileage');
            $orders->save();
            MaintenanceStationCarStatus::update_station_info(Input::get("order_id"),Input::get("car_model_id"),strtoupper(Input::get("vin","")),Input::get('mileage'),Input::get("insurance"),Input::get("start_off_time"));

            // $order_flows = new OrderFlows;
            // $order_flows->order_id = Input::get('order_id');
            // $order_flows->status =1411;
            // $order_flows->user_id = $sc_user->id;
            // $order_flows->type = 'sc';
            // $order_flows->save();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>Input::get('order_id')));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "lsorder_update_car", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      zwh
     * @DateTime:    2016-5-23 11:58:17
     * @Description: 用于更换基础产品包的耗材型号
     */
    /**
    public function changeBaseProductItem(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'order_id' => 'required',
                'item_id'=> 'required',//订单耗材的id
                'new_item_id'=> 'required',
                'type' => 'required'//0为标准耗材1为非标
            )
            );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=Orders::find(Input::get('order_id'));
            if(is_null($orders)||$orders->order_type!=2){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            if($orders->status!=1411){//订单状态是正确
                $response = Lang::get("scs_error.order_status_error_40022");
                break;
            }
            $opbi=OrderProductBaseItem::find(Input::get("item_id"));
            if(is_null($opbi)||$opbi->order_id!=$orders->order_id||$opbi->type!=1){
                $response = Lang::get("scs_error.order_product_item_notexit_40011");
                break;
            }
            $order_product=OrderProduct::find($opbi->order_product_package_id);
            if(is_null($order_product)||$order_product->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }
            if($order_product->status!=1){
                $response = Lang::get("scs_error.order_product_status_error_40009");
                break;
            }
            $wmcf = new \StdClass();
            $csi=DataCarStandardInfo::find($orders->car_model_id);
            if(isset($csi)){
                $wmcf=WorkModelCoefficient::where("name",$csi->model_level)->first();
            }else{
                $wmcf->supply_coefficient=1;
            }
            //要更换成的配件类型判断
            if(Input::get('type')==0){
                $mw=MaintenanceWarehouse::where('item_id',Input::get('new_item_id'))->where('shop_id',$sc_user->shop_id)->where('item_type',0)->first();
                if(!$mw || $opbi->num>$mw->number){
                    $response = Lang::get("scs_error.supply_item_low_stocks_40005");
                    break;
                }
                $si=SupplyItem::find(Input::get("new_item_id"));
                if(Orders::isSale($si->id,0)){
                    $response = Lang::get("scs_error.supply_error_10011");
                    break;
                }
                //更换耗材的加价系数
                // $wpc=WorkPartsCoefficient::where("car_model_name",$csi->model_level)->where("supply_brand_id",$si->brand_id)->where("supply_subject_id",$si->subject)->where("status",1)->first();
                // if($wpc){
                //     $wmcf->supply_coefficient=$wpc->coefficient;
                // }else{
                //     $wmcf->supply_coefficient=1;
                // }
                $wpc=WorkPartsCoefficient::where("car_model_name",$csi->model_level)->where("supply_brand_id",$si->brand_id)->where("supply_subject_id",$si->subject)->where("status",1)->first();
                $wmcf->supply_coefficient = null;
                if (!empty($wpc)) {
                    $wpc_city=WorkPartsCoefficientCity::where('city_id',$sc_user->shop->city_id)->where('coefficient_id',$wpc->id)->where('status',1)->first();
                    $wmcf->supply_coefficient=isset($wpc_city->coefficient) ? $wpc_city->coefficient : null; //如果有城市系数，优先使用。
                }
                if ($wmcf->supply_coefficient == null){ //如果是0，说明没有赋值
                    $wmcf->supply_coefficient=isset($wpc->coefficient) ? $wpc->coefficient : 1;
                }
                //更换多城市价格
                $sip=SupplyItemPrice::where("item_id",$si->id)->where("city_id",$sc_user->shop->city_id)->first();
                if($sip){
                    $si->amount=$sip->price;
                }
            }else{
                $mw=MaintenanceWarehouse::where('item_id',Input::get('new_item_id'))->where('shop_id',$sc_user->shop_id)->where('item_type',1)->first();
                if(!$mw || $opbi->num>$mw->number){
                    $response = Lang::get("scs_error.supply_item_low_stocks_40005");
                    break;
                }
                $max=MaintenanceStorageItem::where('item_id',Input::get('new_item_id'))->where('type',1)->where('item_type',1)->where('current_number','>',0)->where('status',1)->max('tax_purchase_price');
                if(Input::get('amount')*100 < $max){
                    $response = Lang::get("scs_error.data_price_error_61018");
                    break;
                }
                $si=SupplyItemNonStandard::find(Input::get("new_item_id"));
                if(Orders::isSale($si->id,1)){
                    $response = Lang::get("scs_error.supply_error_10011");
                    break;
                }
                $si->amount=Input::get("amount")*100;//如果是非标的 更新非标为传过来的价格
            }

            //原有配件类型判断
            if($opbi->item_type==0){
                $old_si=SupplyItem::find($opbi->item_id);
            }else{
                $old_si=SupplyItemNonStandard::find($opbi->item_id);
            }
            if(is_null($si)||$si->status!=1){
                $response = Lang::get("scs_error.supply_item_not_exit_41000");
                break;
            }
            if($old_si->subject!=$si->subject){
                $response = Lang::get("scs_error.supply_subject_error_41002");
                break;
            }

            // $number=Input::get("number");
            // if($number==$opbi->num){
            //     $response = array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>$order_product->order_id,"order_product_amount"=>$order_product->amount,"order_amount"=>$orders->amount));
            //     break;
            // }
            $response=OrderProductBaseItem::changeProductItem($order_product,$opbi,$si,$sc_user->id,$wmcf,Input::get('type'));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "changeBaseProductItem", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    **/

    /**
     * @Author:      zwh
     * @DateTime:    2016-5-23 12:03:48
     * @Description: 用于更换非标服务包的耗材型号
     */
    public function changeNonStandardItem(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                // 'type'   => 'required',
                'order_id' => 'required',
                'order_product_non_standard_id'=> 'required',//supply_item或supply_item_non_standard的id
                'new_item_id'=> 'required',//supply_item或supply_item_non_standard的id
            )
            );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=Orders::find(Input::get('order_id'));
            if(is_null($orders)||$orders->order_type!=2){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            if($orders->status!=1411){//订单状态是正确
                $response = Lang::get("scs_error.order_status_error_40022");
                break;
            }
            $opns=OrderProductNonStandard::where('id',Input::get("order_product_non_standard_id"))->where('status',1)->first();
            if(is_null($opns)||$opns->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_item_notexit_40011");
                break;
            }
            $order_product=OrderProduct::find($opns->order_product_id);
            if(is_null($order_product)||$order_product->order_id!=$orders->order_id){
                $response = Lang::get("scs_error.order_product_notexit_40006");
                break;
            }
            if($order_product->status!=1){
                $response = Lang::get("scs_error.order_product_status_error_40009");
                break;
            }
            $wmcf=new \stdClass();
            $csi=DataCarStandardInfo::find($orders->car_model_id);
            if(isset($csi)){
                $wmcf=WorkModelCoefficient::where("name",$csi->model_level)->first();
            }else{
                $wmcf->supply_coefficient=1;
            }
            $si=SupplyItem::find(Input::get("new_item_id"));
            // if(is_null($si)||$si->status!=1){//如果 耗材不存在或者 状态不正确
            //     $response = Lang::get("scs_error.supply_item_not_exit_41000");
            //     break;
            // }

            if($opns->type==0&&$opns->item_id!=Input::get('new_item_id')){//说明原来 是标准耗材切 更换了耗材
                OrderSmyFilling::where("order_id",$orders->order_id)->where("product_item_id",$opns->id)->where("product_type","non_standard")->update(array("status"=>0));//取消原来的的耗材的添注记录
            }            
            // if(Input::get('type')==0){
                $mw=MaintenanceWarehouse::where('item_id',$si->id)->where('shop_id',$sc_user->shop_id)->first();
                Log::info($mw);
                if(!$mw || $opns->num>$mw->number){
                    $response = Lang::get("scs_error.supply_item_low_stocks_40005");
                    break;
                }
                // if($opns->type==0){
                $old=SupplyItem::find($opns->item_id);
                // }else{
                //     $old=SupplyItemNonStandard::find($opns->item_id);
                // }
                // if(is_null($si)||$si->status!=1){//如果 耗材不存在或者 状态不正确
                //     $response = Lang::get("scs_error.supply_item_not_exit_41000");
                //     break;
                // }

                if($old->supply_classification_id!=$si->supply_classification_id){
                    $response = Lang::get("scs_error.supply_subject_error_41002");
                    break;
                }
                // $spi=SupplyProductItem::where("car_model_id",$csi->id)->where("supply_item_id",$si->id)->where("status",1)->first();
                // if(is_null($spi)){
                //     $response = Lang::get("scs_error.supply_item_car_model_error_41001");
                //     break;
                // }

                // $ss=SupplySeries::find($si->series_id);
                $moo=MaintenanceOutOrder::where("product_type","non_standard")->where("product_item_id",$opns->id)->whereIn("status",array(0,1,2))->first();
                if($moo){//如果存在 备货中或者 等待领料的 出库申请 已经领料的 不允许修改
                    return Lang::get("scs_error.order_product_item_change_error_40012");
                }
                if($si->type==1||PriceBargaining::ischange($si->id,$orders->shop_id)==1){//如果非标耗材和 标准耗材能编辑价格的
                    $c_amount=Input::get("amount")*100;
                    $max=MaintenanceStorageItem::where('shop_id',$orders->shop_id)->where('item_id',Input::get('new_item_id'))->where('type',1)->where('current_number','>',0)->where('status',1)->max('tax_purchase_price');
                    if($c_amount < $max){
                        $response = Lang::get("scs_error.data_price_error_61018");
                        break;
                    }
                }else{ 
                    // $sip=SupplyItemPrice::where("item_id",$si->id)->where("city_id",$sc_user->shop->city_id)->first();
                    // if($sip){
                    //     $si->amount=$sip->price;
                    // }
                    //新多城市价格
                    $sip=SupplyCityPrice::where("supply_id",$si->id)->where("city_id",$orders->city)->where("status",1)->first();
                    if($sip){
                        $si->amount=$sip->price;
                    }else{
                        $ss_temp=SupplySub::where("supply_id",$si->id)->where("status",1)->first();
                        $si->amount=isset($ss_temp)?$ss_temp->usual_price:0;
                    }
                    $c_amount=Common::upFow($si->amount*$wmcf->supply_coefficient);
                }
                //更换耗材的加价系数
                // $wpc=WorkPartsCoefficient::where("car_model_name",$csi->model_level)->where("supply_brand_id",$si->brand_id)->where("supply_subject_id",$si->subject)->where("status",1)->first();
                // if($wpc){
                //     $wmcf->supply_coefficient=$wpc->coefficient;
                // }else{
                //     $wmcf->supply_coefficient=1;
                // }
                 //更换多城市价格
                
                //判断特殊油品
                if(in_array($si->id,Config::get("smy.item_id"))){//如果新耗材是特殊油品
                    OrderSmyFilling::saveosf($orders->shop_id,$si->id,"non_standard",$opns->id,$orders->order_id);
                } 

                $old_amount=$opns->num*$opns->amount;                
                $new_amount=$opns->num*$c_amount;
                $temp_amount=$new_amount-$old_amount;
                $discount=0;
                if($opns->discount!=1){
                    $discount=$opns->num*$opns->amount*(1-$opns->discount);
                }
                $opr=new OrderProductRecord();
                $opr->order_id=$orders->order_id;
                $opr->product_type='non_standard';
                $opr->operation_type='update';
                $opr->num=$opns->num;
                $opr->operation_grade='二级';
                $opr->user_id=$sc_user->id;
                $opr->user_type='sc';
                $opr->operation_id=$opns->id;
                $opr->name=$opns->name;
                $opr->amount=$opns->amount;
                $opr->save();

                $item_info=SupplyItem::getCacheItemInfo($si->id);
                $opns->item_id=$si->id;
                $opns->name=$item_info->name;
                $opns->amount=$c_amount;
                $opns->type=$si->type;
                $opns->unit=$item_info->unit?$item_info->unit:'';
                $opns->discount=1;
                $opns->save();

                //每次添加标准或非标配件都需修改order_product及orders中金额
                $order_product->amount+=$temp_amount;
                $order_product->discount=$order_product->discount-$discount;
                $order_product->save();
                $orders->amount+=$temp_amount;
                $orders->balance_amount=$orders->balance_amount+$temp_amount-$discount;
                $orders->discount=$orders->discount-$discount;
                $orders->save();
            // }else{
            //     $mw=MaintenanceWarehouse::where('item_id',Input::get('new_item_id'))->where('shop_id',$sc_user->shop_id)->where('item_type',1)->first();
            //     if(!$mw || $opns->num>$mw->number){
            //         $response = Lang::get("scs_error.supply_item_low_stocks_40005");
            //         break;
            //     }
            //     $max=MaintenanceStorageItem::where('item_id',Input::get('new_item_id'))->where('type',1)->where('item_type',1)->where('current_number','>',0)->where('status',1)->max('tax_purchase_price');
            //     if(Input::get('amount')*100 < $max){
            //         $response = Lang::get("scs_error.data_price_error_61018");
            //         break;
            //     }
            //     $sins=SupplyItemNonStandard::find(Input::get("new_item_id"));
            //     $sins->amount=Input::get("amount")*100;//如果是非标的 更新非标为传过来的价格
            //     if($opns->type==0){
            //         $old=SupplyItem::find($opns->item_id);
            //     }else{
            //         $old=SupplyItemNonStandard::find($opns->item_id);
            //     }
            //     if(is_null($sins)||$sins->status!=1){//如果 耗材不存在或者 状态不正确
            //         $response = Lang::get("scs_error.supply_item_not_exit_41000");
            //         break;
            //     }
            //     if($old->subject!=$sins->subject){
            //         $response = Lang::get("scs_error.supply_subject_error_41002");
            //         break;
            //     }
            //     // if($sins->id==$opns->item_id){//如果没有更换耗材
            //     //     $response = array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>$order_product->order_id,"order_product_amount"=>$order_product->amount,"order_amount"=>$orders->amount));
            //     //     break;
            //     // }

            //     $moo=MaintenanceOutOrder::where("product_type","non_standard")->where("product_item_id",$opns->id)->whereIn("status",array(0,1,2))->first();
            //     if($moo){//如果存在 备货中或者 等待领料的 出库申请 已经领料的 不允许修改
            //         return Lang::get("scs_error.order_product_item_change_error_40012");
            //     }
            //     $old_amount=$opns->num*$opns->amount;
            //     $new_amount=$opns->num*$sins->amount;
            //     $temp_amount=$new_amount-$old_amount;
            //     $discount=0;
            //     if($opns->discount!=1){
            //         $discount=$opns->num*$opns->amount*(1-$opns->discount);
            //     }

            //     $opr=new OrderProductRecord();
            //     $opr->order_id=$orders->order_id;
            //     $opr->product_type='non_standard';
            //     $opr->operation_type='update';
            //     $opr->num=$opns->num;
            //     $opr->operation_grade='二级';
            //     $opr->user_id=$sc_user->id;
            //     $opr->user_type='sc';
            //     $opr->operation_id=$opns->id;
            //     $opr->name=$opns->name;
            //     $opr->amount=$opns->amount;
            //     $opr->save();


            //     $opns->name=$sins->series_name." ".$sins->model;
            //     $opns->item_id=Input::get('new_item_id');
            //     $opns->amount=$sins->amount;
            //     $opns->type=Input::get('type');
            //     $opns->unit=$sins->unit;
            //     $opns->discount=1;
            //     $opns->save();

            //     //每次添加标准或非标配件都需修改order_product及orders中金额
            //     $order_product->amount+=$temp_amount;
            //     $order_product->discount=$order_product->discount-$discount;
            //     $order_product->save();
            //     $orders->amount+=$temp_amount;
            //     $orders->balance_amount=$orders->balance_amount+$temp_amount-$discount;
            //     $orders->discount=$orders->discount-$discount;
            //     $orders->save();
            // }
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "getProductPackages", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      zwh
     * @DateTime:    2016-5-23 12:06:32
     * @Description: 用于添加 配件
     */
    public function addPart(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required',
                // 'type' => 'required',
                "item_id"=>'required',//supply_item的id
                'number'=> 'required|numeric|min:0|not_in:0',
                'order_product_id'=> 'required',
                'amount'=> 'min:0'
            )
            );
        $number=Input::get('number');
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=Orders::find(Input::get('order_id'));
            if(is_null($orders) ||$orders->order_type!=2){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.order_note_shop_error_40003");
                break;
            }
            if($orders->status!=1411){//订单状态是正确
                $response = Lang::get("scs_error.order_status_error_40022");
                break;
            }

            $si=SupplyItem::find(Input::get("item_id"));
            // if(is_null($si)||$si->status!=1){//如果 耗材不存在或者 状态不正确
            //     $response = Lang::get("scs_error.supply_item_not_exit_41000");
            //     break;
            // }

            $mw=MaintenanceWarehouse::where('item_id',Input::get('item_id'))->where('shop_id',$sc_user->shop_id)->first();
            if(!$mw || $number>$mw->number){
                $response = Lang::get("scs_error.supply_item_low_stocks_40005");
                break;
            }
            $wmcf=new \stdClass();
            $csi=DataCarStandardInfo::find($orders->car_model_id);
            if(isset($csi)){
                $wmcf=WorkModelCoefficient::where("name",$csi->model_level)->first();
            }else{
                $wmcf->supply_coefficient=1;
            }
            $op=OrderProduct::find(Input::get('order_product_id'));
            // if(Input::get('type')==0){
                //if(isset($csi)){
                //    $wmcf=WorkModelCoefficient::where("name",$csi->model_level)->first();
                //}else{
                //    $wmcf->supply_coefficient=1;
                //}
                
                if(Orders::isSale($si->id,$si->type)){
                    $response = Lang::get("scs_error.supply_error_10011");
                    break;
                }
                if($si->type==1||PriceBargaining::ischange($si->id,$orders->shop_id)==1){//如果能编辑价格
                    $si->amount=Input::get("amount")*100;
                    $max=MaintenanceStorageItem::where('shop_id',$orders->shop_id)->where('item_id',Input::get('item_id'))->where('type',1)->where('current_number','>',0)->where('status',1)->max('tax_purchase_price');
                    if($si->amount< $max){
                        return Lang::get("scs_error.data_price_error_61018");
                    }
                }else{  
                    //更换耗材的加价系数
                    //$wpc=WorkPartsCoefficient::where("car_model_name",$csi->model_level)->where("supply_brand_id",$si->brand_id)->where("supply_subject_id",$si->subject)->where("status",1)->first();
                    //if($wpc){
                    //    $wmcf->supply_coefficient=$wpc->coefficient;
                    //}else{
                    //    $wmcf->supply_coefficient=1;
                    //}
                    //更换多城市价格
                    // $sip=SupplyItemPrice::where("item_id",$si->id)->where("city_id",$sc_user->shop->city_id)->first();
                    // if($sip){
                    //     $si->amount=$sip->price;
                    // }
                    $sip=SupplyCityPrice::where("supply_id",$si->id)->where("city_id",$orders->city)->where("status",1)->first();
                    if($sip){
                        $si->amount=$sip->price;
                    }else{
                        $ss_temp=SupplySub::where("supply_id",$si->id)->where("status",1)->first();
                        $si->amount=isset($ss_temp)?$ss_temp->usual_price:0;
                    }
                    $c_amount=Common::upFow($si->amount*$wmcf->supply_coefficient);
                }
                 
                

                $item_info=SupplyItem::getCacheItemInfo($si->id);
                $temp_amount=Common::upFow($si->amount);//零售单独配件不计算加价系数，产品包需要计算
                $opns=new OrderProductNonStandard;
                $opns->order_id=$orders->order_id;
                $opns->name=$item_info->name;
                $opns->num=$number;
                $opns->item_id=$si->id;
                $opns->amount=$temp_amount;
                $opns->order_product_id=Input::get('order_product_id');
                $opns->work_station=0;
                $opns->work_status="prepare";
                $opns->type=$si->type;
                $opns->service_type=1;
                $opns->unit=$item_info->unit?$item_info->unit:'';
                $opns->save();
                //判断特殊油品
                if(in_array($si->id,Config::get("smy.item_id"))){//如果新耗材是特殊油品
                    OrderSmyFilling::saveosf($orders->shop_id,$si->id,"non_standard",$opns->id,$orders->order_id);
                } 
                //每次添加标准或非标配件都需修改order_product及orders中金额
                $op->amount+=$number*$temp_amount;
                $op->save();

                $orders->amount+=$number*$temp_amount;
                $orders->balance_amount+=$number*$temp_amount;
                $orders->save();
            // }else{
            //     $sins=SupplyItemNonStandard::find(Input::get("item_id"));
            //     if(is_null($sins)||$sins->status!=1){//如果 耗材不存在或者 状态不正确
            //         $response = Lang::get("scs_error.supply_item_not_exit_41000");
            //         break;
            //     }
            //     if(Orders::isSale($sins->id,1)){
            //         $response = Lang::get("scs_error.supply_error_10011");
            //         break;
            //     }
            //     $max=MaintenanceStorageItem::where('item_id',Input::get('item_id'))->where('type',1)->where('item_type',1)->where('current_number','>',0)->where('status',1)->max('tax_purchase_price');
            //     if(Input::get('amount')*100 < $max){
            //         $response = Lang::get("scs_error.data_price_error_61018");
            //         break;
            //     }
            //     $sins->amount=Input::get("amount")*100;
            //     $opns=new OrderProductNonStandard;
            //     $opns->order_id=$orders->order_id;
            //     $opns->name=$sins->series_name." ".$sins->model;
            //     $opns->num=$number;
            //     $opns->item_id=$sins->id;
            //     $opns->amount=$sins->amount;
            //     $opns->order_product_id=Input::get('order_product_id');
            //     $opns->work_station=0;
            //     $opns->work_status="prepare";
            //     $opns->type=Input::get('type');
            //     $opns->service_type=1;
            //     $opns->unit=$sins->unit;
            //     $opns->save();

            //     //每次添加标准或非标配件都需修改order_product及orders中金额
            //     $op->amount+=$number*$sins->amount;
            //     $op->save();

            //     $orders->amount+=$number*$sins->amount;
            //     $orders->balance_amount+=$number*$sins->amount;
            //     $orders->save();
            // }
            $opr=new OrderProductRecord();
            $opr->order_id=$orders->order_id;
            $opr->product_type='non_standard';
            $opr->operation_type='create';
            $opr->num=$number;
            $opr->operation_grade='二级';
            $opr->user_id=$sc_user->id;
            $opr->user_type='sc';
            $opr->operation_id=$opns->id;
            $opr->name=$opns->name;
            $opr->amount=$opns->amount;
            $opr->save();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>Input::get('order_id')));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "addPart", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      zwh
     * @DateTime:    2016-5-24 10:43:35
     * @Description: 创建关联订单
     */
    public function create_relations(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'ls_order_id' => 'required',
                'order_id' => 'required',
            )
            );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $orders=Orders::find(Input::get('order_id'));
            if(is_null($orders)){
                $response = Lang::get("scs_error.order_notexiet_40000");
                break;
            }
            if($orders->order_type!=1){
                $response = Lang::get("scs_error.order_relation_error_40036");
                break;
            }
            $or=OrderRelations::where('order_id',Input::get('order_id'))->first();
            if($or){
                $or->mirror_order_id=Input::get('ls_order_id');
                $or->save();
            }else{
                $or = new OrderRelations();
                $or->order_id=Input::get("order_id");
                $or->mirror_order_id=Input::get('ls_order_id');
                $or->save();
            }
            $response = array('code' => 0, 'msg' => 'ok','result' => array('order_id'=>Input::get('order_id')));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "ls_order_id", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}